{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 元组与序列\n",
    "元组由多个用逗号隔开的值组成"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12345, 54321, 'hello!')\n",
      "12345\n",
      "((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))\n",
      "(1, 2, 3, 4, 5)\n",
      "([1, 2, 3], [3, 2, 1])\n"
     ]
    }
   ],
   "source": [
    "t = 12345,54321, 'hello!'\n",
    "print(t)\n",
    "print(t[0])\n",
    "u = t, (1, 2, 3, 4, 5)\n",
    "print(u)\n",
    "print(u[1])\n",
    "v = ([1, 2, 3], [3, 2, 1])\n",
    "print(v)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "输出时，元组都要由圆括号标注，这样才能正确地解释嵌套元组。\n",
    "输入时，圆括号可有可无，不过经常是必须的。\n",
    "不允许为元组中的单个元素赋值。\n",
    "元组时不可变的， 一般包含异质元素序列，通过解包或索引访问。\n",
    "列表是可变的，一般包含同质元素徐磊，可迭代访问。"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "()\n",
      "('hello',)\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# 构建0个或1个元素的元组\n",
    "empty = ()\n",
    "print(empty)\n",
    "singleton = 'hello',\n",
    "print(singleton)\n",
    "print(len(singleton))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12345, 54321, 'hello')\n",
      "12345\n",
      "54321\n",
      "hello\n"
     ]
    }
   ],
   "source": [
    "# 元组打包\n",
    "t = 12345, 54321, 'hello'\n",
    "print(t)\n",
    "# 序列解包\n",
    "x, y, z = t\n",
    "print(x)\n",
    "print(y)\n",
    "print(z)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}